/*
 * CaseInstructionDescription.java
 * 
 * last update: 30.01.2010 by Vlad Badoiu
 * 
 * author:	Vlad Badoiu(vladbadoiu@gmail.com)
 *
 * Obs:
 */

package engine;

import java.util.*;

@SuppressWarnings("serial")
class CaseInstructionDescription extends InstructionDescription{
	ExpressionDescription ed;
	Vector<Object> choices;
	int type;

	CaseInstructionDescription(int db, int line, NameSpaceDescription nsd,
			int type, ExpressionDescription ed, Vector<Object> v){
		super(db, line, nsd);
		this.ed = ed;  this.type = type;  choices = v;
	}

	DefaultInstruction instantiate(NameSpace ns)throws ParseException{

		Vector<Object> v = new Vector<Object>();
		InstructionDescription id;
		ExpressionDescription edt;
		Expression edInstance;

		for (Enumeration<Object> e = choices.elements(); e.hasMoreElements(); ) {
			Object o = e.nextElement();
			if (o instanceof ExpressionDescription) {
				edt = (ExpressionDescription) o;
				v.addElement(edt == null ? null : edt.instantiate(ns)); 
			} else { //since the parser allows only these two types, the following
				//cast is ok
				id = (InstructionDescription) o;
				v.addElement(id == null ? null : id.instantiate(ns));
			}
		}

		try{
			edInstance = ed.instantiate(ns);
		}catch(ParseException ex){
			xConsole.dumpStack(ex);
			throw new ParseException(super.toString() + " error:" + ex);
		}

		return new CaseInstruction(ns, delayBefore, this, type, edInstance, v,
				next == null ? null : next.instantiate(ns));
	}

	public String toString(){
		return super.toString() + "case (" + ed + "):";
	}
}












